package util;

import com.alibaba.fastjson.JSON;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class BTCUtil {
    public static void main(String[] args) throws IOException {
//       getBTCAlerts();
//        BTCPrice();
    }

    //一分钟抓取一次
    public static void BTCPrice() throws IOException {
        Document document = Jsoup.connect("https://m.chaintiao.com/api/json.php?type=all&id=BTC").ignoreContentType(true).get();
        Map<String, Object> map = JSON.parseObject(document.text(), Map.class);
        List<List<Object>> data = (List<List<Object>>) map.get("data");
        List<Object> list = data.get(data.size() - 1);
        System.out.println(list.get(0));//时间戳
        System.out.println(list.get(1));//开盘
        System.out.println(list.get(2));//最高
        System.out.println(list.get(3));//最低
        System.out.println(list.get(4));//收盘
        System.out.println(list.get(7));//成交额//美元
        System.out.println(list);
    }

    //1小时抓取一次，凌晨可以2小时抓一次
    //获取7*24资讯
    public static void getBTCAlerts() throws IOException {
        Document document = Jsoup.connect("https://www.528btc.com/kx/").ignoreContentType(true).get();
//        System.out.println(document.text());
        Elements elements = document.getElementsByClass("layui-timeline-item ");
        for (Element element : elements) {
//            System.out.println(element.text());
            String time = element.getElementsByClass("ktime").text();
            String title = element.getElementsByClass("kh").text();
            String link = "https://www.528btc.com/" + element.getElementsByTag("a").attr("href");
            String content = element.getElementsByTag("p").text();
            System.out.println(time);//时间
            System.out.println(title);//标题
            System.out.println(link);//链接
            System.out.println(content);//内容
        }
    }
}
